/**
 * $Revision: 1.8 $
 * $Author: geln_yang $
 * $Date: 2009/06/15 04:36:24 $
 */
package c4j.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * 
 */
public class DateUtil {

	private static final String yyyy_MM_dd = "yyyy-MM-dd";

	private static final String yyyyMMdd = "yyyyMMdd";

	/**
	 * parse with the format: yyyy/MM/dd
	 */
	public static String getTodayDateValue() {
		return new SimpleDateFormat(yyyyMMdd).format(new Date());
	}

	public static Date parseDate(String dateStr) {
		SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_dd);
		try {
			return sdf.parse(dateStr);
		} catch (ParseException e) {
			return null;
		}
	}

	public static Date parseDate(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_dd);
		String dateStr = sdf.format(date);
		return parseDate(dateStr);
	}

	public static int getDifferDays(String strBegin, String strEnd) {
		try {
			SimpleDateFormat yMd_sdf = new SimpleDateFormat(yyyy_MM_dd);
			return getDifferDays(yMd_sdf.parse(strBegin), yMd_sdf.parse(strEnd));
		} catch (ParseException e) {
			return 0;
		}
	}

	public static int getDifferDays(Date begin, Date end) {
		return (int) ((end.getTime() - begin.getTime()) / (24 * 60 * 60 * 1000));
	}

	public static String getTWDate(String date) {
		if (date == null)
			return null;
		try {
			SimpleDateFormat yMd_sdf = new SimpleDateFormat(yyyyMMdd);
			return getTWDate(yMd_sdf.parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String getTWDate(Date date) {
		SimpleDateFormat yMdsdf = new SimpleDateFormat(yyyyMMdd);
		String dateStr = "";
		dateStr = yMdsdf.format(date);
		int year = Integer.parseInt(dateStr.substring(0, 4));
		year -= 1911;
		if (year < 0)
			throw new IllegalArgumentException("The date[" + date + "] is less than 1911!");

		String res = "00" + year + dateStr.substring(4, 8);
		return res.substring(res.length() - 7);
	}

	public static Date getFirstMonthDay(Date date) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DATE));
		return calendar.getTime();
	}

	public static Date getLastMonthDay(Date date) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
		return calendar.getTime();
	}

	public static List<Date> getAllDates(Date start, Date end) {
		Calendar calendar1 = new GregorianCalendar();
		calendar1.setTime(start);
		Calendar calendar2 = new GregorianCalendar();
		calendar2.setTime(end);
		List<Date> list = new ArrayList<Date>();
		list.add(start);
		while (calendar1.compareTo(calendar2) < 0) {
			calendar1.add(Calendar.DATE, 1);
			list.add(calendar1.getTime());
		}
		return list;
	}

	public static String getZHDateString(Date date) {
		return new SimpleDateFormat("yyyy'\u5e74'MM'\u6708'dd'\u65e5'").format(date);
	}

	public static String getZHTimeString(Date date) {
		return new SimpleDateFormat("HH\u65f6mm'\u5206'ss'\u79d2'").format(date);
	}

	public static void main(String[] args) {
		System.out.println(getZHDateString(new Date()));
		System.out.println(getZHTimeString(new Date()));
		try {
			String start = "2008-07-10";
			String end = "2008-07-23";
			int last = DateUtil.getDifferDays(start, end);
			// assertEquals(13, last);
			System.out.println(last);

			SimpleDateFormat yMd_sdf = new SimpleDateFormat(yyyy_MM_dd);
			Date startD = yMd_sdf.parse("2008-07-30");
			Date endD = yMd_sdf.parse("2008-08-1");
			last = DateUtil.getDifferDays(startD, endD);
			// assertEquals(2, last);
			System.out.println(last);

			System.out.println(getTWDate("19110822 "));
			System.out.println(getTWDate("20080822 "));
			System.out.println(getTWDate("20060822 12:12:12"));
			System.out.println(getTWDate(new Date()));
			System.out.println(getTWDate("19100822 "));

		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
}
